<div data-role="roleGrid"></div>
<script>
	$(function(){
		var tabData = $('[data-role="roleGrid"]').closest('.tab-pane.active').data();
		var userId = tabData.userId;
		var userAccount = tabData.userAccount;
		var columns = [ 
					{
						title : "角色名称",
						name : "name",
						width : 250
					}, 
					{
						title : "角色描述",
						name : "roleDesc",
						width : 250
					}
				];
		var getButtons = function(){
			if (userId) {
				return [
					{content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-th-large"><span>分配角色</button>', action: 'assignRole'},
					{content: '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>', action: 'removeRoleForUser'}
					];
			} else {
				return [
					{content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-plus"><span>添加</button>', action: 'add'},
					{content: '<button class="btn btn-success" type="button"><span class="glyphicon glyphicon-edit"><span>修改</button>', action: 'modify'},
					{content: '<button class="btn btn-danger" type="button"><span class="glyphicon glyphicon-remove"><span>删除</button>', action: 'delete'},
					{content: '<button class="btn btn-info" type="button"><span class="glyphicon glyphicon-user"><span>用户管理</button>', action: 'assignUser'},
						{content: '<button class="btn btn-primary" type="button"><span class="glyphicon glyphicon-th"><span>资源授权</button>', action: 'assignResource'}
				];
			}
		};
		var getQuerys = function(){
			if(userId){
				return [];
			}else{
				return [{title: '角色名称', value: 'roleNameForSearch'}];
			}
		}
		var url = contextPath + '/auth/Role/pageJson.koala';
		if(userAccount){
			url += '?userAccount='+userAccount;
		}
		$('[data-role="roleGrid"]').grid({
			 identity: 'id',
             columns: columns,
             buttons: getButtons(),
             querys: getQuerys(),
             url: url
        }).on({
        	'add': function(){
        		roleManager().add($(this));
        	},
        	'modify': function(event, data){
        		var indexs = data.data;
	            var $this = $(this);
	            if(indexs.length == 0){
	                $this.message({
	                    type: 'warning',
	                    content: '请选择一条记录进行修改'
	                })
	                return;
	            }
	            if(indexs.length > 1){
	                $this.message({
	                    type: 'warning',
	                    content: '只能选择一条记录进行修改'
	                })
	                return;
	            }
				roleManager().modify(data.item[0], $(this));
        	},
        	'delete': function(event, data){
        		var indexs = data.data;
	            var $this = $(this);
        		if(indexs.length == 0){
		            $this.message({
		                   type: 'warning',
		                    content: '请选择要删除的记录'
		            })
		             return;
	            }
	            $this.confirm({
	                content: '确定要删除所选记录吗?',
	                callBack: function(){roleManager().deleteUser(data.item, $this);}
	            });
        	},
        	'assignRole': function(){
	            roleManager().assignRole(userId, userAccount, $(this));
        	},
        	'removeRoleForUser': function(event, data){
        		var indexs = data.data;
	            var $this = $(this);
        		if(indexs.length == 0){
		            $this.message({
		                   type: 'warning',
		                    content: '请选择要删除的记录'
		            })
		             return;
	            }
	            $this.confirm({
	                content: '确定要删除所选记录吗?',
	                callBack: function(){roleManager().removeRoleForUser(userId, data.item, $this);}
	            });
        	},
        	'assignUser': function(event, data){
        		var indexs = data.data;
	            var $this = $(this);
	            if(indexs.length == 0){
	                $this.message({
	                    type: 'warning',
	                    content: '请选择一条记录进行操作'
	                })
	                return;
	            }
	            if(indexs.length > 1){
	                $this.message({
	                    type: 'warning',
	                    content: '只能选择一条记录进行操作'
	                })
	                return;
	            }
	            roleManager().assignUser(data.data[0], data.item[0].name);
        	},
        	'assignResource': function(event, data){
        		var indexs = data.data;
	            var $this = $(this);
	            if(indexs.length == 0){
	                $this.message({
	                    type: 'warning',
	                    content: '请选择一条记录进行操作'
	                })
	                return;
	            }
	            if(indexs.length > 1){
	                $this.message({
	                    type: 'warning',
	                    content: '只能选择一条记录进行操作'
	                })
	                return;
	            }
	            roleManager().assignResource(data.data[0]);
        	}
        });
	})
</script>